<?php
/**
 * @link https://ym2.cleverstone.top
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

use builder\helper\H5;
use yii\helpers\Json;

/** @var \yii\web\View $this */
/* @var string $id 当前树表格组件ID */
/* @var array $ttRowOperation 行操作项 */
/* @var array $ttToolbarCustom 自定义头部工具栏 ['left' => [...], 'right' => [...]] */
/* @var array $ttToolbarFilter 树表格筛选 */
/* @var array $ttToolbarTab 工具选项卡 */
/** @var array $rootLayout 根布局 */
/** @var array $extraDataTpl 额外数据展示模板 */
?>

<div class="card border-0 <?= $rootLayout['class'] ?>"
     ng-controller="ym2.ctrl.<?= $id ?>"
     style="<?= $rootLayout['style'] ?>"
    <?= $rootLayout['attribute'] ?>>
    <div class="card-header bg-white border-bottom-0 pt-4">
        <!--筛选模板 start -->
        <?php if (!empty($ttToolbarFilter)): ?>

            <form class="layui-row layui-col-space16 mx-0" ng-show="ym2TreeTableFilterShow_<?= $id ?>">
                <?php foreach ($ttToolbarFilter['columns'] as $field => $column): ?>
                    <?php switch ($column['control']): case 'datetime':?>
                    <?php case 'date':?>
                    <?php case 'year':?>
                    <?php case 'month':?>
                    <?php case 'time':?>

                        <!--datetime/date/year/month/time-->
                        <div class="layui-input-group layui-col-sm3 px-0 <?= $column['class'] ?>">
                            <?php if (!empty($column['label'])): ?>

                                <span class="layui-input-prefix text-right ym2-control-prefix"><?= $column['label'] ?></span>
                            <?php endif; ?>

                            <input type="text"
                                   style="<?= $column['style'] ?>"
                                   <?= $column['attribute'] ?>
                                   name="<?= $field ?>"
                                   ng-model="ym2FilterForm['<?= $field ?>']"
                                   ym2-laydate
                                   ym2-range="<?= $column['range'] ?>"
                                   ym2-type="<?= $column['control'] ?>"
                                   placeholder="<?= $column['placeholder'] ?>"
                                   class="layui-input layui-input-sm"
                                   autocomplete="off"
                                   readonly="readonly">
                        </div>
                        <?php break; case 'select':?>

                        <!--select-->
                        <div class="layui-input-group layui-col-sm3 px-0 <?= $column['class'] ?>" style="<?= $column['style'] ?>">
                            <?php if (!empty($column['label'])): ?>

                                <span class="layui-input-prefix text-right ym2-control-prefix"><?= $column['label'] ?></span>
                            <?php endif; ?>

                            <select ui-select2="<?= H5::encode(Json::encode($column['uiConfig'])) ?>"
                                    class="invisible"
                                    <?= $column['attribute'] ?>
                                    name="<?= $field ?>"
                                    <?= $column['multiple'] ? 'multiple' : '' ?>
                                    ng-model="ym2FilterForm['<?= $field ?>']"
                                    data-placeholder="<?= $column['placeholder'] ?>">
                                <?php if (empty($column['multiple'])): ?>

                                    <option value=""><?= $column['placeholder'] ?></option>
                                <?php endif; ?>
                                <?php foreach ($column['options'] as $value => $text): ?>
                                    <?php if (is_array($text)): ?>

                                        <optgroup label="<?= $value ?>">
                                            <?php foreach ($text as $vv => $vTxt): ?>

                                                <option <?= in_array($vv, $column['disabled']) ? 'disabled' : '' ?> value="<?= $vv ?>">
                                                    <?= $vTxt ?>
                                                </option>
                                            <?php endforeach; ?>

                                        </optgroup>
                                    <?php else: ?>

                                        <option <?= in_array($value, $column['disabled']) ? 'disabled' : '' ?> value="<?= $value ?>">
                                            <?= $text ?>
                                        </option>
                                    <?php endif; ?>
                                <?php endforeach; ?>

                            </select>
                        </div>
                        <?php break; case 'select_link':?>

                        <!--select link-->
                        <div class="layui-input-group layui-col-sm3 px-0 <?= $column['class'] ?>" style="<?= $column['style'] ?>">
                            <?php if (!empty($column['label'])): ?>

                                <span class="layui-input-prefix text-right ym2-control-prefix"><?= $column['label'] ?></span>
                            <?php endif; ?>
                            <?php if (!empty($column['options'])): // start ?>

                                <select ui-select2="<?= H5::encode(Json::encode($column['uiConfig'])) ?>"
                                        class="invisible"
                                        <?= $column['attribute'] ?>
                                        name="<?= $field ?>"
                                        ng-change="selectLinkRequest('<?= $field ?>', '<?= $column["targetLink"] ?>', '<?= H5::encode($column["route"]) ?>')"
                                        ng-model="ym2FilterForm['<?= $field ?>']"
                                        data-placeholder="<?= $column['placeholder'] ?>">
                                    <option value=""><?= $column['placeholder'] ?></option>
                                    <?php foreach ($column['options'] as $value => $text): ?>
                                        <?php if (is_array($text)): ?>

                                            <optgroup label="<?= $value ?>">
                                                <?php foreach ($text as $vv => $vTxt): ?>

                                                    <option <?= in_array($vv, $column['disabled']) ? 'disabled' : '' ?> value="<?= $vv ?>">
                                                        <?= $vTxt ?>
                                                    </option>
                                                <?php endforeach; ?>

                                            </optgroup>
                                        <?php else: ?>

                                            <option <?= in_array($value, $column['disabled']) ? 'disabled' : '' ?> value="<?= $value ?>">
                                                <?= $text ?>
                                            </option>
                                        <?php endif; ?>
                                    <?php endforeach; ?>

                                </select>
                            <?php elseif (!empty($column["targetLink"])): // through ?>

                                <select ui-select2="<?= H5::encode(Json::encode($column['uiConfig'])) ?>"
                                        class="invisible"
                                        <?= $column['attribute'] ?>
                                        name="<?= $field ?>"
                                        ng-change="selectLinkRequest('<?= $field ?>', '<?= $column["targetLink"] ?>', '<?= H5::encode($column["route"]) ?>')"
                                        ng-model="ym2FilterForm['<?= $field ?>']"
                                        data-placeholder="<?= $column['placeholder'] ?>">
                                    <option value=""><?= $column['placeholder'] ?></option>
                                    <optgroup ng-if="!<?= $field ?>__options.length"
                                              label="{{index}}"
                                              ng-repeat="(index, item) in <?= $field ?>__options track by $index">
                                        <option ng-repeat="son in item track by $index"
                                                value="{{son.value}}"
                                                ng-disabled="son.disabled"
                                                ng-bind-html="son.text | toHtml">
                                        </option>
                                    </optgroup>
                                    <option ng-if="<?= $field ?>__options.length"
                                            ng-repeat="item in <?= $field ?>__options track by $index"
                                            value="{{item.value}}"
                                            ng-disabled="item.disabled"
                                            ng-bind-html="item.text | toHtml">
                                    </option>
                                </select>
                            <?php else: // end ?>

                                <select ui-select2="<?= H5::encode(Json::encode($column['uiConfig'])) ?>"
                                        class="invisible"
                                        <?= $column['attribute'] ?>
                                        name="<?= $field ?>"
                                        ng-model="ym2FilterForm['<?= $field ?>']"
                                        data-placeholder="<?= $column['placeholder'] ?>">
                                    <option value=""><?= $column['placeholder'] ?></option>
                                    <optgroup ng-if="!<?= $field ?>__options.length"
                                              label="{{index}}"
                                              ng-repeat="(index, item) in <?= $field ?>__options track by $index">
                                        <option ng-repeat="son in item track by $index"
                                                value="{{son.value}}"
                                                ng-disabled="son.disabled"
                                                ng-bind-html="son.text | toHtml">
                                        </option>
                                    </optgroup>
                                    <option ng-if="<?= $field ?>__options.length"
                                            ng-repeat="item in <?= $field ?>__options track by $index"
                                            value="{{item.value}}"
                                            ng-disabled="item.disabled"
                                            ng-bind-html="item.text | toHtml">
                                    </option>
                                </select>
                            <?php endif; ?>

                        </div>
                        <?php break; case 'text':?>

                        <!--text-->
                        <div class="layui-input-group layui-col-sm3 px-0 <?= $column['class'] ?>">
                            <?php if (!empty($column['label'])): ?>

                                <span class="layui-input-prefix text-right ym2-control-prefix"><?= $column['label'] ?></span>
                            <?php endif; ?>

                            <input type="text"
                                   name="<?= $field ?>"
                                   ng-model="ym2FilterForm['<?= $field ?>']"
                                   <?= $column['attribute'] ?>
                                   style="<?= $column['style'] ?>"
                                   placeholder="<?= $column['placeholder'] ?>"
                                   class="layui-input layui-input-sm"
                                   autocomplete="off">
                        </div>
                        <?php break; case 'number':?>

                        <!--number-->
                        <div class="layui-input-group layui-col-sm3 px-0 <?= $column['class'] ?>">
                            <?php if (!empty($column['label'])): ?>

                                <span class="layui-input-prefix text-right ym2-control-prefix"><?= $column['label'] ?></span>
                            <?php endif; ?>

                            <input type="number"
                                   string-to-number
                                   name="<?= $field ?>"
                                   ng-model="ym2FilterForm['<?= $field ?>']"
                                   <?= $column['attribute'] ?>
                                   style="<?= $column['style'] ?>"
                                   placeholder="<?= $column['placeholder'] ?>"
                                   class="layui-input layui-input-sm"
                                   autocomplete="off">
                        </div>
                        <?php break; endswitch; ?>
                <?php endforeach; ?>

                <div class="layui-btn-container layui-col-sm2 pb-0 px-0">
                    <button type="button" class="layui-btn layui-btn-sm layui-btn-primary" ng-click="resetFilter()">重置</button>
                    <button class="layui-btn layui-btn-sm layui-btn-theme" ng-click="submitFilter()">筛选</button>
                </div>
            </form>
        <?php endif; ?>

        <!--筛选模板 end -->
    </div>
    <div class="card-body pt-0">
        <!--树表格模板 start -->
        <?= /* @var \builder\tree_table\TreeTableBuilder $this->context */ $this->context->beginTreeTable() ?>

        <!--选项卡筛选-->
        <?php if (!empty($ttToolbarTab)): ?>
        <div class="layui-tab layui-tab-brief only-title">
            <ul class="layui-tab-title">
                <?php foreach ($ttToolbarTab as $key => $item): ?>
                <li class="<?= !empty($item['defaultChecked']) ? 'layui-this' : null ?>" ng-click="triggerTab(<?= H5::encode(Json::encode($item['params'])) ?>)">
                    <?= !empty($item['icon']) ? '<i class="' . $item['icon'] . '"></i>' : '' ?>
                    <?= !empty($item['title']) ? $item['title']: '' ?>
                </li>
                <?php endforeach; ?>
            </ul>
        </div>
        <?php endif; ?>
        <?php if (!empty($extraDataTpl)): ?>

         <?= $this->renderPhpFile($extraDataTpl['tpl'], ['cols' => $extraDataTpl['cols']]) ?>
        <?php endif; ?>

        <table class="layui-hide" id="ym2_builder_<?= $id ?>"></table>
        <!--树表格模板 end -->
        <?= /* @var \builder\tree_table\TreeTableBuilder $this->context */ $this->context->endTreeTable() ?>

    </div>

    <!-- 头部工具栏模板 start -->
    <?php if (!empty($ttToolbarCustom['left'])): ?>

        <script type="text/html" id="ym2_toolbar_<?= $id ?>">
            <div class="layui-btn-container">
                <?php foreach ($ttToolbarCustom['left'] as $item): ?>

                    <button class="layui-btn layui-btn-sm <?= $item['btnClass'] ?>" lay-event="<?= $item['options']['__event_id'] ?>">
                        <?php if (!empty($item['icon'])): ?>

                            <span class="<?= $item['icon'] ?>"></span>
                        <?php endif; ?>

                        <?= $item['title'] ?>

                    </button>
                <?php endforeach; ?>

            </div>
        </script>
    <?php endif; ?>

    <!-- 头部工具栏模板 end -->

    <!--行工具栏模板 start -->
    <script type="text/html" id="ym2_tool_<?= $id ?>">
        <div class="layui-btn-container">
            <?php if (!empty($ttRowOperation['normal'])): ?>
                <?php foreach ($ttRowOperation['normal'] as $item): ?>
                    <?php if (!empty($operationId = $item['options']['operationId'])): // ID存在定义 ?>

                        {{#  if(d["<?= $operationId ?>"]){ }}
                        <button class="layui-btn layui-btn-xs layui-btn-primary f12" lay-event="<?= $item['options']['__event_id'] ?>">
                            <?php if (!empty($item['options']['icon'])): ?>

                                <span class="<?= $item['options']['icon'] ?>"></span>
                            <?php endif; ?>

                            <?= $item['options']['title'] ?>

                        </button>
                        {{#  } }}
                    <?php else: // ID未定义 ?>

                        <button class="layui-btn layui-btn-xs layui-btn-primary f12" lay-event="<?= $item['options']['__event_id'] ?>">
                            <?php if (!empty($item['options']['icon'])): ?>

                                <span class="<?= $item['options']['icon'] ?>"></span>
                            <?php endif; ?>

                            <?= $item['options']['title'] ?>

                        </button>
                    <?php endif; ?>
                <?php endforeach; ?>
            <?php endif; ?>

            <?php if (!empty($ttRowOperation['more'])): ?>

                <button class="layui-btn layui-btn-xs layui-btn-theme" lay-event="ym2_row_event_group_more">
                    更多
                    <i class="layui-icon layui-icon-down"></i>
                </button>
            <?php endif; ?>

        </div>
    </script>
    <!--行工具栏模板 end -->
</div>